package com.j3l11234.myy_sign.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.j3l11234.myy_sign.entity.UserEntity;
import com.j3l11234.myy_sign.model.UserModel;
import com.j3l11234.myy_sign.util.MyException;
import com.j3l11234.myy_sign.util.PrivilegeCheck;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserModel userModel;
	
	@RequestMapping("/login")
    public Object login() { 
        return "user/login";
    }
	
	@RequestMapping(value = "/doLogin",method = RequestMethod.POST)
	@ResponseBody
	public Object doLogin(
			@RequestParam(value ="username") String username,
			@RequestParam(value ="password") String password,
			HttpSession session){
		Map<String, Object> retMap = new HashMap<String, Object>();
		
		if(username.equals("")){
			retMap.put("success", 0);
			retMap.put("data", "请输入用户名");
			return retMap;
		}
		if(password.equals("")){
			retMap.put("success", 0);
			retMap.put("data", "请输入密码");
			return retMap;
		}
		
		try {
			UserEntity userEntity = userModel.login(username, password);
			session.setAttribute("user", userEntity);
			retMap.put("success", 1);
			retMap.put("data", "登录成功");
		} catch (MyException e) {
			retMap.put("success", 0);
			retMap.put("data", e.getMessage());
			return retMap;
		}
		
		return retMap;
	}
	
	@RequestMapping("/logout")
    public Object logout(HttpSession session) { 
		session.removeAttribute("user");
        return "redirect:/user/login.do";
    }
	
	
	@RequestMapping("/edit")
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object edit(
    		@RequestParam(value="user", required = true) int user_id,
    		ModelMap model, HttpSession session) {
		model.addAttribute("navbar_item", "admin");
		model.addAttribute("sidebar_item", "user");
		
		UserEntity userEntity = null;
		if(user_id != 0){
			try {
				userEntity = userModel.getUserByID(user_id);
			} catch (MyException e) {
				userEntity = new UserEntity();
			}
		}else{
			userEntity = new UserEntity();
		}
		
		model.addAttribute("editUser", userEntity);
        return "user/edit";
    }
	
	@RequestMapping(value="/edit",  method = RequestMethod.POST)
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object postedit(
    		@RequestParam(value="user_id", required = true) int user_id,
    		@RequestParam(value="username", required = true) String username,
    		@RequestParam(value="password", required = true) String password,
    		@RequestParam(value="alias", required = true) String alias,
    		@RequestParam(value="privilege", required = true) int privilege,
    		ModelMap model,HttpSession session) {
		
		UserEntity userEntity = null;
		try {
			if(user_id > 0){
				userEntity = userModel.editUser(user_id, username, password, alias, privilege);
			}else{
				userEntity = userModel.addUser(username, password, alias, privilege);
			}
			
			model.addAttribute("result", true);
			model.addAttribute("resultText", "提交成功");
		} catch (MyException e) {
			model.addAttribute("result", false);
			model.addAttribute("resultText", e.getMessage());
			userEntity = new UserEntity();
			e.printStackTrace();
		}
		
		return edit(userEntity.getId() ,model, session);	
    }
	
	@RequestMapping(value="/delete")
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object delete(
    		@RequestParam(value="user", required = true) int user_id,
    		ModelMap model,HttpSession session) {

		try {
			userModel.deleteUser(user_id);
			model.addAttribute("result", true);
			model.addAttribute("resultText", "删除成功");
		} catch (MyException e) {
			model.addAttribute("result", false);
			model.addAttribute("resultText", e.getMessage());
			e.printStackTrace();
		}
		
		return edit(0 ,model, session);
		
    }
	
	
}
